package com.huarui.company.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.company.service.CompanyPermissionService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

@SuppressWarnings("serial")
public class CompanyPermissionAction extends BaseAction {

	@SuppressWarnings("unchecked")
	private ListModel listModel=new ListModel();
	private Map<String, Object> resultMap = new HashMap<String, Object>();
	private CompanyPermissionService companyPermissionService;
	private String node;
	private Integer roleId;
	private String rolesJson;
	private SessionMessage sm;
	private List<Map> list;
	
	private Integer start;
	private Integer limit;
	@SuppressWarnings("unchecked")
	public String treeLoader(){
		sm=UserHolder.getCurrentSessionMessage();
		list=companyPermissionService.comPerMissionTreeLoader(sm,node);
		return "list";
	}
	@SuppressWarnings("unchecked")
	public String getRoleList(){
		sm=UserHolder.getCurrentSessionMessage();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", sm.getCompanyId());
		list=companyPermissionService.getRoleListByCondition(map);
		return "list";
	}
	@SuppressWarnings("unchecked")
	public String getEmployeeList(){
		sm=UserHolder.getCurrentSessionMessage();
		listModel.setPage(start == null ? 0 : start / limit + 1);
		listModel.setPageSize(limit == null ? 10 : limit);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", sm.getCompanyId());
		map.put("userId", sm.getUserId());
		map.put("roleId", roleId);
		List<Map> list=companyPermissionService.getEmployeeList(listModel,map);
		resultMap.put("root", list);
		resultMap.put("totalProperty", listModel.getTotal());
		return Action.SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String save(){
		sm=UserHolder.getCurrentSessionMessage();
		List<Map> roleList=(List<Map>) JSONArray.toCollection(JSONArray.fromObject(rolesJson), HashMap.class);
		resultMap=companyPermissionService.saveCompanyPermission(sm, roleList,roleId);
		return Action.SUCCESS;
	}
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	public void setListModel(ListModel listModel) {
		this.listModel = listModel;
	}

	
	public void setNode(String node) {
		this.node = node;
	}
	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}
	public List<Map> getList() {
		return list;
	}
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}
	public void setRolesJson(String rolesJson) {
		this.rolesJson = rolesJson;
	}
	public void setCompanyPermissionService(
			CompanyPermissionService companyPermissionService) {
		this.companyPermissionService = companyPermissionService;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	
}
